package com.ileaf.pattern01.observable;

import com.ileaf.pattern01.observer.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察对象
 */
public abstract class Observable {

    /**
     * 观察者容器
     */
    public final List<Observer> observerList = new ArrayList<>();

    /**
     * 注册观察者
     */
    public void registerObserver(Observer observer) {
        if (!observerList.contains(observer)) {
            observerList.add(observer);
        }
    }

    public abstract void notifyObserver(Observer observer);

    /**
     *
     * @param observer
     */
    public void notifyAllObserver(Observer ... observer){
        for (Observer obs : observer) {
            obs.update();
        }
    }
}
